<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/article_category/to/article_category_to.php");
require_once("model/article_category/dao/article_category_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Article_category_dao_test {

	private $dao;
	private $autor;
	
	public function __construct(){}
	
	public function before_test() {	
		try {
		
			$this->dao = Autor_dao_factory::singleton()->get_dao();
			$this->autor = new Autor_to('xxxxxxxxxxxx','password','email');
			$this->dao->create($this->autor);
			$this->dao = Article_category_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			$e->getMessage() . '</br>';
		}
	}
	
	public function run_test() {
		try {
			$article_category_to = new Article_category_to(0,'title', 'description', 
														$this->autor->get_login(), 0);
			
			//create
			$article_category_to = (object) $this->dao->create($article_category_to);	
			echo 'create article_category: ' . $article_category_to->to_string() . '<br/>';
			
			//create 2
			$article_category_to2 = new Article_category_to(0,'title3', 'description', 
														$this->autor->get_login(), 0);
			$article_category_to2 = (object) $this->dao->create($article_category_to2);	
			
			//create existent 
			try {
				$this->dao->create($article_category_to);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//exist inexistent 
			if (!$this->dao->exist(9999))
				echo 'No exist article 9999<br/>';
			else 
				throw new Exception("Error, exist article_category inexistent");
			
			//exist existent 
			$this->dao->exist($article_category_to->get_number());
			echo 'exist article_category: ' . $article_category_to->to_string() . '<br/>';
			
			//exist inexistent 
			if (!$this->dao->exist_by_title('xxxxxxxxxxxxxxxxxx'))
				echo 'No exist article 9999<br/>';
			else 
				throw new Exception("Error, exist title by article_category inexistent");
				
			//exist inexistent by title 
			$this->dao->exist_by_title('xxxxxxxxxxxxxxxxxx');
			echo 'No exist by title article_category xxxxxxxxxxxxxxxxxx<br/>';
			
			//exist existent by title
			$this->dao->exist_by_title($article_category_to->get_title());
			echo 'exist by title article_category: ' . $article_category_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_number_article_category(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find existent 
			$aux_article_category_to = $this->dao->find_by_number_article_category($article_category_to->get_number());	
				if ($article_category_to->equals($aux_article_category_to)) 
					echo 'found article_category: ' . $article_category_to->to_string() . '<br/>';
				else
					echo 'not equal article_category inserted';
					
			//find 20 most importants					
			$category_list = $this->dao->find_most_importants(1);
					echo 'Most importants categories:</br>';
					foreach ($category_list as $category)
						echo '    Category: ' . $category->to_string() . '</br>';
					
			//find all			
			$category_list = $this->dao->find_all();
					echo 'All categories:</br>';
					foreach ($category_list as $category)
						echo '    Category: ' . $category->to_string() . '</br>';
				
			//count articles by autor
			$categories = $this->dao->count_categories_by_login($this->autor->get_login());
			echo 'Count categories by autor: ' . $categories . '</br>';
				
			//update 
			$article_category_to->set_description("description2");
			$this->dao->update($article_category_to);
			$aux_article_category_to = $this->dao->find_by_number_article_category($article_category_to->get_number());
			echo 'update article: ' . $aux_article_category_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999);
				throw new Exception('Removed inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '<br/>';
			}
			
			//remove existent 
			$this->dao->remove($article_category_to->get_number());
			echo 'remove article: ' . $article_category_to->to_string() . '<br/>';
			
		}
		catch (Duplicate_instance_exception $e) {
			echo $e->getMessage();
		}
		catch (Internal_error_exception $e) {
			echo $e->getMessage();
		}
		catch (Instance_not_found_exception $e) {
			echo $e->getMessage();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}

//test
$test = new Article_category_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>